<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var person1 = {
        name: "yan1",
        age: 18,
      };
      var person2 = {
        nam: "yan2",
        age: "18",
      };
      var propertyName = "name";
      console.log(person1[propertyName]); //yan1

      console.log(person1.name); //yan1
      console.log(person1["name"]); //yan1
      console.log(person1.age); //18
      console.log(person1["age"]); //18

      //   console.log(person2[nam]); //nam is undefine
      console.log(person2["name"]); // undefine
      console.log(person2.age); //18
      console.log(person2["age"]); //18
      console.log(person2[name]); // undefined
      console.log(person2);
      //console.log(person2[age]); //age is not defined ??? 为什么person2[name]不报错 而person2[age]报错?
      // name是关键字,在全局默认存在一个name属性，且值为空串
      //使用方括号语法时,应该将要访问的属性以字符串的形式放在方括号中,使用方括号语法的主要优点是可以通过变量来访问属性。

      const obj = {
        age: 25,
        city: "New York",
      };

      // name 变量未定义

      console.log(obj[name]); // 报错: ReferenceError: name is not defined
    </script>
  </body>
</html>
